<!doctype html public "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd">
<html>
<head>
<title>
 Overview
</title>
<link rel ="stylesheet" type="text/css" href="../stylesheet.css" title="Style">
<script>
function asd() {
	
		parent.document.title="Serializer.jsc Overview";
	
}
</script>
</head>
<body bgcolor="white" onload="asd();">

<!-- ========== START OF NAVBAR ========== -->
<a name="navbar_top"><!-- --></a>
<table border="0" width="100%" cellpadding="1" cellspacing="0">
<tr>
<td colspan=2 bgcolor="#EEEEFF" class="NavBarCell1">
<a name="navbar_top_firstrow"><!-- --></a>
<table border="0" cellpadding="0" cellspacing="3">
  <tr align="center" valign="top">
  
  
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="../overview-summary.html"><font class="NavBarFont1"><b>Overview</b></font></a>&nbsp;</td>
  <td bgcolor="#FFFFFF" class="NavBarCell1Rev">	&nbsp;<font class="NavBarFont1Rev"><b>File</b></font>&nbsp;</td>
  

  <td bgcolor="#FFFFFF" class="NavBarCell1"> 	<font class="NavBarFont1">Class</font>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="../overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="../index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="../help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a>&nbsp;</td>
  </tr>
</table>
</td>
<td bgcolor="#EEEEFF" align="right" valign="top">
<em>
<b></b></em>
</td>
</tr>

<tr>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
&nbsp;PREV&nbsp;
&nbsp;NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
  <a href="../index.html" target="_top"><b>FRAMES</b></a>  &nbsp;
&nbsp;<a href="../overview-summary.html" target="_top"><b>NO FRAMES</b></a>
&nbsp;&nbsp;
<script>
  <!--
  if(window==top) {
    document.writeln('<A HREF="allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
  }
  //-->
</script>
<noscript>
<a href="../allclasses-noframe.html" target=""><b>All Classes</b></a>
</noscript>
</font></td>
</tr>
</table>
<!-- =========== END OF NAVBAR =========== -->

<hr>
<center>
	
	   <h2>Serializer.jsc</h2>
	
</center>

	


<h4>Summary</h4>
<p>
	
		js.io.Serializer class http://jsvm.homolo.com/jsvm2.0/docs/api/
 <BR/><BR/><B>Version: </B>2.01, 10/23/05
 <BR/><BR/><B>Author:</B> Wan Changhua
 <BR/>
	
</p>

<hr>


    <table border="1" cellpadding="3" cellspacing="0" width="100%">
    <tr bgcolor="#CCCCFF" class="TableHeadingColor">
    <td colspan=2><font size="+2">
    
        <b>Class Summary</b>
    
    </font></td>
    </tr>
    
    <tr bgcolor="white" class="TableRowColor">
    <td width="15%"><b><a href="Serializer.html">js.io.Serializer</a></b></td>
    <td>js.io.Serializer is a utility class for serialize
 </td>
    </tr>
    
    </table>
    <hr/> 


<!-- ========== METHOD SUMMARY =========== -->

<!-- ========== END METHOD SUMMARY =========== -->


        <pre class="sourceview"># language: JSVM2

<span class="comment">/**
 * <span class="attrib">@fileoverview</span> js.io.Serializer class http://jsvm.homolo.com/jsvm2.0/docs/api/
 * <span class="attrib">@file</span>		Serializer.jsc
 * <span class="attrib">@author</span>	Wan Changhua
 * <span class="attrib">@version</span>	2.01, 10/23/05
 * <span class="attrib">@since</span>		JSVM1.0
 */</span>

package js.io;

import js.lang.JObject;
import js.io.IOException;
import js.net.XmlDom;
import js.util.HashMap;

<span class="comment">/**
 * Create a new Serializer instance.
 * Inherit from JObject
 * <span class="attrib">@author</span>	Wan Changhua
 * <span class="attrib">@version</span>	2.01, 10/23/05
 * <span class="attrib">@extends</span> JObject
 * <span class="attrib">@class</span> js.io.Serializer is a utility class for serialize
 * <span class="attrib">@constructor</span>
 * <span class="attrib">@final</span>
 * <span class="attrib">@deprecated</span>
 */</span><span class="reserved">function</span> Serializer()
{

	var objectMap = new js.util.HashMap();
	var sequence = 0;
	var __this = <span class="reserved">this</span>;

	var serializeToNode = <span class="reserved">function</span> (obj)
	{
		var node = null;
		var type = typeof(obj);
		switch(type)
		{
			case <span class="literal">"undefined"</span>	:	node = trsfmer.createElement(<span class="literal">"void"</span>); break;
			case <span class="literal">"number"</span>	: node = trsfmer.createElement(<span class="literal">"number"</span>, obj); break;
			case <span class="literal">"string"</span>	: node = trsfmer.createElement(<span class="literal">"string"</span>, obj); break;
			case <span class="literal">"boolean"</span>	: node = trsfmer.createElement(<span class="literal">"boolean"</span>, String(obj).toLowerCase()); break;
			case <span class="literal">"function"</span>	: node = trsfmer.createElement(<span class="literal">"function"</span>, obj.toString()); break;
			case <span class="literal">"object"</span>	:
				<span class="reserved">if</span> (obj == null) {
					node = trsfmer.createElement(<span class="literal">"null"</span>);
				} <span class="reserved">else</span> <span class="reserved">if</span> (obj instanceof Array) {
					node = trsfmer.createElement(<span class="literal">"array"</span>);
					<span class="reserved">for</span> (var i = 0; i &lt; obj.length; i++) {
						var _cnode = serializeToNode(obj[i]);
						node.appendChild(_cnode);
					}
				} <span class="reserved">else</span> {
					node = trsfmer.createElement(<span class="literal">"object"</span>);
					<span class="reserved">if</span> (obj instanceof Date) {
						node.setAttribute(<span class="literal">"type"</span>, <span class="literal">"Date"</span>);
						node.appendChild(trsfmer.createTextNode(obj.getTime()));
					} <span class="reserved">else</span> <span class="reserved">if</span> (obj instanceof Error) {
						node.setAttribute(<span class="literal">"type"</span>, <span class="literal">"Error"</span>);
						node.appendChild(trsfmer.createElement(<span class="literal">"number"</span>, obj.number));
						node.appendChild(trsfmer.createElement(<span class="literal">"description"</span>, obj.description));
					} <span class="reserved">else</span> <span class="reserved">if</span> (obj instanceof String) {
						node.setAttribute(<span class="literal">"type"</span>, <span class="literal">"String"</span>);
						node.appendChild(trsfmer.createTextNode(obj.toString()));
					} <span class="reserved">else</span> <span class="reserved">if</span> (obj instanceof Boolean) {
						node.setAttribute(<span class="literal">"type"</span>, <span class="literal">"Boolean"</span>);
						node.appendChild(trsfmer.createTextNode(obj.valueOf()));
					} <span class="reserved">else</span> <span class="reserved">if</span> (obj instanceof Number) {
						node.setAttribute(<span class="literal">"type"</span>, <span class="literal">"Number"</span>);
						node.appendChild(trsfmer.createTextNode(obj.valueOf()));
					} <span class="reserved">else</span> <span class="reserved">if</span> (obj instanceof RegExp) {
						node.setAttribute(<span class="literal">"type"</span>, <span class="literal">"RegExp"</span>);
						node.appendChild(trsfmer.createTextNode(obj.source));
					} <span class="reserved">else</span> <span class="reserved">if</span> (obj instanceof Object) {
						<span class="reserved">if</span> (obj instanceof JObject) {
							node.setAttribute(<span class="literal">"type"</span>, obj.getClass().getName());
						} <span class="reserved">else</span> {
							node.setAttribute(<span class="literal">"type"</span>, <span class="literal">"Object"</span>);
						}
						var oid = objectMap.get(obj);
						<span class="reserved">if</span> (oid != null) {
							node.setAttribute(<span class="literal">"ref"</span>, oid);
							break;
						} <span class="reserved">else</span> {
							oid = sequence++ ;
							objectMap.put(obj, oid);
							node.setAttribute(<span class="literal">"oid"</span>, oid);
						}
						<span class="reserved">for</span> (var p in obj) {
							var v = obj[p];
							<span class="reserved">if</span> (typeof(v) == <span class="literal">"function"</span>) continue;
							var pnode = trsfmer.createElement(<span class="literal">"member"</span>,
								__this._serialize(v));
							pnode.setAttribute(<span class="literal">"name"</span>, p);
							node.appendChild(pnode);
						}
					} <span class="reserved">else</span> {
						node.setAttribute(<span class="literal">"type"</span>, <span class="literal">"unknow"</span>);
					}
				}
				break;
			case <span class="literal">"unknow"</span>	:
			default		: node = trsfmer.createElement(<span class="literal">"unknow"</span>);
		}
		<span class="reserved">return</span> node;
	}

	var unserializeFromNode = <span class="reserved">function</span>(node)
	{
		var type = node.tagName;
		switch (type) {
			case <span class="literal">"void"</span>	: <span class="reserved">return</span> void(0);
			case <span class="literal">"null"</span>	: <span class="reserved">return</span> null;
			case <span class="literal">"number"</span>	: <span class="reserved">return</span> isNaN(node.text) ? Number.NaN : eval(node.text);
			case <span class="literal">"string"</span>	: <span class="reserved">return</span> node.text;
			case <span class="literal">"boolean"</span>	: var v = new Boolean(node.text); <span class="reserved">return</span> v.valueOf();
			case <span class="literal">"function"</span>	: <span class="reserved">return</span> new Function(node.text);
			case <span class="literal">"array"</span>	:
				var nodes = node.childNodes, l = nodes.length, arr = [];
				<span class="reserved">for</span> (var i = 0; i &lt; l; i++) {
					arr[i] = unserializeFromNode(nodes[i]);
				}
				<span class="reserved">return</span> arr;
			case <span class="literal">"object"</span>	:
				var otype = node.getAttribute(<span class="literal">"type"</span>);
				<span class="reserved">if</span> (otype == <span class="literal">"Date"</span>) {
					<span class="reserved">return</span> new Date(parseInt(node.text, 10));
				} <span class="reserved">else</span> <span class="reserved">if</span> (otype == <span class="literal">"Error"</span>) {
					<span class="reserved">return</span> new Exception(
						parseInt(node.selectSingleNode(<span class="literal">"number"</span>).text),
						node.selectSingleNode(<span class="literal">"description"</span>).text
					);
				} <span class="reserved">else</span> <span class="reserved">if</span> (otype == <span class="literal">"String"</span>) {
					<span class="reserved">return</span> new String(node.text);
				} <span class="reserved">else</span> <span class="reserved">if</span> (otype == <span class="literal">"Boolean"</span>) {
					<span class="reserved">return</span> new Boolean(node.text);
				} <span class="reserved">else</span> <span class="reserved">if</span> (otype == <span class="literal">"Number"</span>) {
					<span class="reserved">return</span> new Number(node.text);
				} <span class="reserved">else</span> <span class="reserved">if</span> (otype == <span class="literal">"RegExp"</span>) {
					<span class="reserved">return</span> new RegExp(node.text);
				} <span class="reserved">else</span> <span class="reserved">if</span> (otype == <span class="literal">"unknow"</span>) {
					<span class="reserved">return</span> new Object(); <span class="comment">//TODO</span>
				} <span class="reserved">else</span> {
					var ref = node.getAttribute(<span class="literal">"ref"</span>);
					<span class="reserved">if</span> (ref != null) {
						<span class="reserved">return</span> objectMap.get(ref);
					}
					var obj = (otype == <span class="literal">"Object"</span>) ?
						{} : Class.forName(otype).newInstance();
					var oid = node.getAttribute(<span class="literal">"oid"</span>);
					objectMap.put(oid, obj);
					var cnodes = node.selectNodes(<span class="literal">"member"</span>);
					<span class="reserved">for</span> (var i = 0; i &lt; cnodes.length; i++) {
						var cnode = cnodes[i];
						var name = cnode.getAttribute(<span class="literal">"name"</span>);
						var value = __this._unserialize(cnode.text);
						<span class="reserved">if</span> (typeof(value) != <span class="literal">"undefined"</span>)	{
							obj[name] = value;
						}
					}
					<span class="reserved">return</span> obj;
				}
			case <span class="literal">"unknow"</span>	:
			default		:
					throw new IOException(Serializer.getName()
						+ <span class="literal">": unserializeFromNode error; unknow data type."</span>);
		}
	}

	<span class="comment">/**
	 * serialize an object to string
	 * <span class="attrib">@private</span>
	 * <span class="attrib">@param</span> {Object} obj an object to be serialized.
	 * <span class="attrib">@return</span> a string
	 * <span class="attrib">@type</span> String
	 */</span>
	<span class="reserved">this</span>._serialize = <span class="reserved">function</span>(obj)
	{
		var node;
		<span class="reserved">return</span> (null == (node = serializeToNode(obj))) ?
			null : node.xml;
	}

	<span class="comment">/**
	 * unserialize a string to object
	 * <span class="attrib">@private</span>
	 * <span class="attrib">@param</span> {String} str a string to be unserialized.
	 * <span class="attrib">@return</span> an object
	 * <span class="attrib">@type</span> Object
	 */</span>
	<span class="reserved">this</span>._unserialize = <span class="reserved">function</span>(str)
	{
		var node = trsfmer.transfromXMLToElement(str);
		<span class="reserved">if</span> (node != null)
		{
				<span class="reserved">return</span> unserializeFromNode(node);
		}
		throw new IOException(Serializer.getName()
			+ <span class="literal">": _unserialize() error; dataformat error."</span>);
	}

}

<span class="comment">/**
 * a tool object for transform
 * <span class="attrib">@private</span>
 * <span class="attrib">@type</span> Object
 */</span>
var trsfmer = new <span class="reserved">function</span> ()
{

	var xmldom = js.net.XmlDom.create();
	xmldom.async = false;

	<span class="reserved">this</span>.createElement = <span class="reserved">function</span>(name, text)
	{
		var node = xmldom.createElement(name);
		<span class="reserved">if</span> (typeof(text) != <span class="literal">"undefined"</span>)
		{
			node.appendChild(xmldom.createTextNode(String(text)));
		}
		<span class="reserved">return</span> node;
	}
	
	<span class="reserved">this</span>.createTextNode = <span class="reserved">function</span>(text)
	{
		<span class="reserved">return</span> xmldom.createTextNode(String(text));
	}

	<span class="reserved">this</span>.transfromXMLToElement = <span class="reserved">function</span>(xml)
	{
		<span class="reserved">return</span> (xmldom.loadXML(xml)) ?
			xmldom.documentElement : null;
	}

}



<span class="comment">/**
 * serialize an object to string
 * <span class="attrib">@param</span> {Object} obj an object to be serialized.
 * <span class="attrib">@return</span> the result of serialized object
 * <span class="attrib">@type</span> String
 */</span>
Serializer.serialize = <span class="reserved">function</span>(obj)
{
	<span class="reserved">return</span> new Serializer()._serialize(obj);
}

<span class="comment">/**
 * unserialize a string to object
 * <span class="attrib">@param</span> {String} str a string
 * <span class="attrib">@return</span> an object
 * <span class="attrib">@type</span> Object
 */</span>
Serializer.unserialize = <span class="reserved">function</span>(str)
{
	<span class="reserved">return</span> new Serializer()._unserialize(str);
}</pre>
	<hr>



<!-- ========== START OF NAVBAR ========== -->
<a name="navbar_top"><!-- --></a>
<table border="0" width="100%" cellpadding="1" cellspacing="0">
<tr>
<td colspan=2 bgcolor="#EEEEFF" class="NavBarCell1">
<a name="navbar_top_firstrow"><!-- --></a>
<table border="0" cellpadding="0" cellspacing="3">
  <tr align="center" valign="top">
  
  
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="../overview-summary.html"><font class="NavBarFont1"><b>Overview</b></font></a>&nbsp;</td>
  <td bgcolor="#FFFFFF" class="NavBarCell1Rev">	&nbsp;<font class="NavBarFont1Rev"><b>File</b></font>&nbsp;</td>
  

  <td bgcolor="#FFFFFF" class="NavBarCell1"> <font class="NavBarFont1">Class</font>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="../overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="../index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="../help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a>&nbsp;</td>
  </tr>
</table>
</td>
<td bgcolor="#EEEEFF" align="right" valign="top"><em>
<b></b></em>
</td>
</tr>

<tr>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
&nbsp;PREV&nbsp;
&nbsp;NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
  <a href="../index.html" target="_top"><b>FRAMES</b></a>  &nbsp;
&nbsp;<a href="../overview-summary.html" target="_top"><b>NO FRAMES</b></a>
&nbsp;&nbsp;
<script>
  <!--
  if(window==top) {
    document.writeln('<A HREF="allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
  }
  //-->
</script>
<noscript>
<a href="../allclasses-noframe.html" target=""><b>All Classes</b></a>
</noscript>
</font></td>
</tr>
</table>
<!-- =========== END OF NAVBAR =========== -->

<hr>
<font size="-1">

</font>
<div class="jsdoc_ctime">Documentation generated by <a href="http://jsdoc.sourceforge.net/" target="_parent">JSDoc</a> on Thu Nov 17 20:10:21 2005</div>
</body>
</html>
